// 作物抽象类
import {CropType, FlowerName, type GardenCropType, type GardenFlowerType} from "@/components/type";
import {ScoreType} from "@/components/ScorePanel/type";
import {reactive} from "vue";
import Score from "@/modal/Score";
import Goods from "@/modal/Goods";
import {store} from "@/modal/store";

const score = reactive(Score.getInstance());
const goods = reactive(Goods.getInstance());

export default abstract class GardenCrop {
  protected abstract type: CropType;
  private currentStage_: number = 0;
  readonly ripeStage: number;
  readonly plotIndex: number;

  protected constructor(options?: any) {
    this.ripeStage = options.ripeStage;
    this.plotIndex = options.plotIndex;
  }

  get currentStep() {
    return this.currentStage_;
  }

  // 是否可种植
  get isPlant() {
    return this.currentStep === 0 ;
  }

  // 是否可灌溉
  get isWater() {
    return this.currentStep > 0 && !this.isRipen;
  }

  // 是否可收获
  get isRipen() {
    return this.currentStep === this.ripeStage;
  }

  growUp() {
    if (!this.isRipen) {
      this.currentStage_++;
    }
    return this;
  }

  getAward() {}
}

// 南瓜类
export class Pumpkin extends GardenCrop {
  protected type = CropType.PUMPKIN;
  readonly goodsNum: number;
  readonly score: number = 0;

  constructor(options: GardenCropType) {
    super(options); // 父构造类有的可传有的可不传，需要加<any>
    this.goodsNum = options?.goodsNum!;
  }

  getAward() {
    if (this.isRipen) {
      goods.gainGoods(this.goodsNum);
    }
    return this;
  }
}


// 玉米类
export class Corn extends GardenCrop {
  protected type: CropType = CropType.CORN;
  readonly score: number = 3;
  readonly goodsNum: number = 0;
  readonly group: number;

  constructor(group: number, plotIndex: number) {
    super({ripeStage: 4, plotIndex: plotIndex});
    this.group = group;
  }

  getAward() {
    if (this.isRipen) {
      score.addScore(this.score, ScoreType.CORN);
    }
    return this;
  }
}

// 豌豆类
export class Pea extends GardenCrop {
  protected type: CropType = CropType.PEA;
  readonly score: number = 1;
  readonly goodsNum: number = 0;
  readonly group: number;
  public isUnlock_: boolean = false;

  constructor(group: number, plotIndex: number) {
    super({ripeStage: 2, plotIndex: plotIndex});
    this.group = group;
  }

  get isPlant() {
    return this.isUnlock_ && this.currentStep === 0;
  }

  unlockPlant(cornSteps: number): boolean {
    return this.isUnlock_ = cornSteps >= 2;
  }

  getAward() {
    if (this.isRipen) {
      score.addScore(this.score, ScoreType.PEA);
    }
    return this;
  }
}

// 花丛类
export class Cluster extends GardenCrop {
  protected type: CropType = CropType.CLUSTER;
  readonly name: FlowerName;
  readonly chineseName: FlowerName;
  readonly direction: string;


  constructor(options: GardenFlowerType) {
    super({ripeStage: 1, plotIndex: options.plotIndex});
    this.name = options.name;
    this.chineseName = options.chineseName;
    this.direction = options.direction;
  }

  // 是否被南瓜激活行动
  get isConnected() {
    return this.isRipen;
  }

  getAward() {
    if (this.isRipen) {
      store.flowers.forEach(flower => {
        if (flower.name === this.name) flower.growUp().getAward();
      });
    }
    return this;
  }
}
